package com.example.forum.exception;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(ApplicationException.class)
    public AppResult ApplicationExceptionHandler(ApplicationException e){
        e.printStackTrace();
        log.error(e.getMessage());
        if(e.getErrResult()!=null){
            return e.getErrResult();
        }
        if(e.getMessage()==null||e.getMessage().equals("")){
            return AppResult.fail(ResultCode.ERROR_SERVICES);
        }
        return AppResult.fail(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public AppResult ExceptionHandler(Exception e){
        e.printStackTrace();
        log.error(e.getMessage());
        if(e.getMessage()==null||e.getMessage().equals("")){
            return AppResult.fail(ResultCode.ERROR_SERVICES);
        }
        return AppResult.fail(e.getMessage());
    }
}
